package com.sqsf.util;

import com.sqsf.javabean.User;
import mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.Date;
import java.util.List;

public class UserTest {
    SqlSession session;
    UserMapper mapper;
    @Before//在所有测试执行之前执行
    public void Before() throws IOException {
        SqlSessionFactory factory = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsStream("mybatis-config.xml"));
        //2、创建SqlSession对象
        session = factory.openSession(true);
        //3、调用session的方法getMapper获取对应接口实现类
        mapper = session.getMapper(UserMapper.class);
    }
    @After//在所有测试执行之后执行
    public void After(){
        //5、关闭资源
        session.close();
    }


    @Test
    public void selectAllUser() throws IOException {
        List<User> users = mapper.selectAllUser();
        //4、打印测试
        for(User u:users){
            System.out.println(u);
        }
    }
    @Test
    public void selectById() throws IOException {
        System.out.println(mapper.selectById(3));
    }
    @Test
    public void selectByIdAndSal() throws IOException {
        System.out.println(mapper.selectByIdAndSal(3,150));
    }
    @Test
    public void insertUser() throws IOException {
        User u=new User();
        u.setName("add");
        u.setSal(400);
        u.setBirthday(new Date());
        mapper.insertUser(u);
    }

    @Test
    public void updateSalById() throws IOException {
        mapper.updateSalById(5,100);
    }

    @Test
    public void deleteById() throws IOException {
        mapper.deleteById(5);
    }
}